package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.shop.ShopBrowseBO;
import com.ztgf.shopping.models.bo.shop.ShopBrowseSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopBrowseDTO;
import com.ztgf.shopping.models.service.shop.IShopBrowseService;
import com.ztgf.shopping.models.vo.shop.ShopBrowseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 用户足迹表对外接口类
 *
 * @author zhanghualang
 * @Date 2020-05-26
 */

@RestController
@RequestMapping("/api/shopBrowse")
@Slf4j
@Api(value = "ShopBrowseController",tags = "用户足迹表接口")
public class ShopBrowseApiController {
    @DubboReference
    private IShopBrowseService ishopBrowseService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找用户足迹表",notes = "根据id查找用户足迹表")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopBrowseDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        ShopBrowseDTO shopBrowseDTO = ishopBrowseService.selectById(id);
        return new Result<>(shopBrowseDTO);
    }

    @ApiOperation(value = "添加用户足迹表",notes = "添加用户足迹表")
    @PostMapping(value = "/insert")
    public Result insert( ShopBrowseSaveBO insertSaveBO) {
        boolean flag = ishopBrowseService.add(insertSaveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "删除用户足迹表",notes = "删除用户足迹表")
    @ApiImplicitParam(name = "id",value = "用户足迹表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = ishopBrowseService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteAll")
    @ApiOperation(value = "删除用户足迹所有表",notes = "批量删除用户足迹表")
    @ApiImplicitParam(name = "shopId",value = "shopId",required = true,paramType = "query", dataType = "String")
    public Result deleteAll(@RequestParam("shopId") String shopId){
        if(StrUtils.isEmpty(shopId)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        Wrapper<ShopBrowseDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", shopId);

        boolean flag = ishopBrowseService.delete(wrapper);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @ApiOperation(value = "用户足迹表列表",notes = "用户足迹表列表")
    @PostMapping("/queryPageList")
    public Result<HashMap<String, ArrayList<ShopBrowseVO>>> queryPageList(ShopBrowseBO shopBrowseBO){
        Page<ShopBrowseDTO> page = new Page<>(shopBrowseBO.getPageNo(), shopBrowseBO.getPageSize());
        HashMap<String, ArrayList<ShopBrowseVO>> map = ishopBrowseService.queryPageList(page, shopBrowseBO);
        return new Result<>(map);
    }

    @ApiOperation(value = "用户足迹表列表",notes = "用户足迹表列表")
    @PostMapping("/queryPageListAll")
    public Result<Page<ShopBrowseVO>> queryPageListAll( ShopBrowseBO shopBrowseBO){
        Page<ShopBrowseVO> page = new Page<>(shopBrowseBO.getPageNo(), shopBrowseBO.getPageSize());
        page = ishopBrowseService.queryPageListAll(page, shopBrowseBO);
        return new Result<>(page);
    }
}
